/**
 * @fileoverview Command object for getting the list of tiers from the server.
 */

goog.provide('dkpoints.rpc.commands.GetTierList');

goog.require('dkpoints.rpc.commands');
goog.require('dkpoints.rpc.cache.TierList');
goog.require('dkpoints.rpc.cache.InstanceTier');

/**
 * Command object for getting a list of tiers with an optional param for
 * matching this instance to a tier
 * @param {function(!Object)} callback Function to pass server's JSON response.
 * @param {string=} instanceName Optional instance name
 * @constructor
 * @extends {dkpoints.rpc.commands.Command}
 */
dkpoints.rpc.commands.GetTierList = function(callback, instanceName) {
  var me = this;

  var url = "/apis/tierlist/";
  if(goog.isDef(instanceName)) {
    url += instanceName;
  }
  
  var commandParams = {
    subclass: "GetTierList",
    url: url,
    callback: callback,
    shouldCache: true,
    shouldBatch: false
  };

  // Call the base class constructor.
  dkpoints.rpc.commands.Command.call(me, commandParams);
  
  if(goog.isDef(instanceName)) {
    /**
     * The instance name to match to the cache list
     * @type {string}
     * @public
     */
    me.instanceName = /** @type {string} */ instanceName;
  }
};
goog.inherits(dkpoints.rpc.commands.GetTierList, dkpoints.rpc.commands.Command);

/**
 * The GetTierList response might actually have two parts that are cached
 * separately if an instance name was given to the constructor.
 * Make sure to get them both.
 * @return {Array} The array of cache names to query
 * @override
 */
dkpoints.rpc.commands.GetTierList.prototype.getCacheNames = function() {
  var me = this;
  
  var cacheNames = [dkpoints.rpc.cache.TierList.buildCacheKey()];
  if(goog.isDef(me.instanceName)) {
    cacheNames.push( dkpoints.rpc.cache.InstanceTier.buildCacheKey(me.instanceName) );
  }
  
  return cacheNames;
};

/**
 * Hand-pick the pieces of the XHR response to give as the command response.
 * Get the cache key(s) and data from the response.  We definitely want to cache the tier list.
 * If an instance name was passed as well, we'll also want to cache the instance to which
 * the tier belongs.
 * We'll return a single object to be cached, or an array of objects to be cached.
 *
 * @param {!Object} xhrResponse The response object returned by the server.
 * @return {Array} An object with 'key' and 'data' fields containing the key & info to cache
 */
dkpoints.rpc.commands.GetTierList.prototype.handleResponse = function(xhrResponse) {
  var me = this;

  // This gets handed back to the calling module through the callback handed to me.
  var cmdResponse = {};
  
  /* An array of single-key/value objects to pass to the cache.
   * Cached items are copied, then the copy is stored in the cache,
   * so we don't have to worry about this being changed. */
  var cmdResponseCacheArray = [];
  
  /* We will definitely want to cache the tier list, which is guaranteed to be in the response. */
  var tierListCacheData = new dkpoints.rpc.cache.TierList(xhrResponse["tiers"]);
  
  // Reference the tier list in both theResponse and the cache array.
  cmdResponse.tierList = tierListCacheData.getData();
  cmdResponseCacheArray.push(tierListCacheData);
  
  // Check if an instanceTier map was in the XHR Response
  var instanceTier = xhrResponse["instanceTier"];
  if(goog.isDef(instanceTier)) {
    var instanceCacheData = new dkpoints.rpc.cache.InstanceTier(me.instanceName, instanceTier);
    
    cmdResponse.instanceTier = instanceCacheData.getData();
    cmdResponseCacheArray.push( instanceCacheData );
  }
    
  // Hand the cmdResponse to the callback provided to the command.
  // This will execute whatever code needs to handle this data.
  me.callback(cmdResponse);
  
  // Once the handling of the data is done, then we can hand the object cache array
  // Back to the RPC module for caching the stuff that needs caching.
  return cmdResponseCacheArray;
};

/**
 * All of the information was found in the cache.  Process and hand it back to the callback.
 * @param {!Object} cacheData An object containing the information in the cache.
 */
dkpoints.rpc.commands.GetTierList.prototype.handleFromCache = function(cacheData) {
  var me = this;

  var cmdResponse = {};
  cmdResponse.tierList = cacheData[ dkpoints.rpc.cache.TierList.buildCacheKey() ].getData();
  
  if(goog.isDef(me.instanceTier)) {
    cmdResponse.instanceTier = cacheData[ dkpoints.rpc.cache.InstanceTier.buildCacheKey(me.instanceName) ].getData();
  }
  
  me.callback(cmdResponse);
};